function formDelete(id){
    if (confirm(signin)){
        var url = "./index.php?type=form&action=delete";
        var post = new Object();
        post["id"] = id;
        $.post(url,post,function(){
            window.location ="index.php?type=form&action=close";
        });
    }        
}

function formUpdate(){
    var url = "./index.php?type=form&action=update";
    var post = new Object();
    post["itemId"] =  $("input#itemId").val();
    $("div.fieldContent :input").each(function(){
        if($(this).attr("asynchronousUpdate") != '1' && this.name != "password_repeat"){
            var fieldId;
            if(this.type == "radio") fieldId = getFieldId($(this).parent().attr('id'));
            else fieldId = getFieldId(this.id);
            post[fieldId] = new Object;
            if(this.type != "button" && this.type != "submit" && $(this).attr("disabled") == false && this.type != "password"){
                if(this.type == "checkbox"){
                   if(this.checked == true)  post[fieldId] = 1;
                   else post[fieldId] = 0;
                }
                else if(this.type == "textarea"){
                    post[fieldId]= tinyMCE.get(this.id).getContent();
                }
                else if(this.type == "radio"){
                    post[fieldId] = $(this +':checked').val();
                }
                else{
                    post[fieldId] = $(this).val();
                }
            }
            else if(this.type == "password"){
                var val1 = $(this).val();
                var val2 = $("#"+this.id+"_repeat").val();
                if(val1.length != 0 && val1 == val2){
                    post[fieldId] = val1;
                }
            }
        }
    });
    $.post(url,post,function(){
        window.location ="index.php?type=form&action=close";
    });
}

function formCreate(){
    var url = "./index.php?type=form&action=create";
    var post = new Object();
    var i = 0;
    $("ul#formCanvas div.fieldContent :input").each(function(){
        if($(this).val() && $(this).attr("asynchronousUpdate") != '1'){
        if(this.type != "button" && this.type != "submit" && $(this).attr("disabled") == false && this.type != "password"){
            post[i] = new Object;
            post[i]['tableName']= $(this).attr("tableName");
            post[i]['columnName'] = this.name;
            post[i]['type'] = this.type;
            if(this.type == "checkbox"){
               if(this.checked == true) post[i]['value'] = 1;
               else post[i]['value'] = 0;
            }
            else if(this.type == "textarea"){
                post[i]['value'] = tinyMCE.get(this.id).getContent();
            }
            else if(this.type == "radio"){
                post[i]['value'] = $(this +':checked').val();
            }
            else{
                post[i]['value'] = $(this).val();
            }
            i++;
        }
        else if(this.type == "password" && this.name != "password_repeat"){
                var val1 = $(this).val();
                var val2 = $("#"+this.id+"_repeat").val();
                if(val1.length != 0 && val1 == val2){
                    post[i] = new Object;
                    post[i]['value'] = val1;
                    post[i]['tableName'] = $(this).attr("tableName");
                    post[i]['columnName'] = this.name;
                    post[i]['type'] = this.type;
                    i++;
                }
        }
        }
    });
    $.post(url,post,function(){
        window.location ="index.php?type=form&action=close";
    });
}

function setFieldPosition(fieldId, direction){
    if(direction == 'up'){
        var upId = $('li.formPart#formPart-'+fieldId).prev().attr('id');
        if(upId != 'undefined'){
            id = getFieldId(upId);
            tinyMCEControl('remove',id);
            $('li#formPart-'+fieldId).after($('li#'+upId));
            tinyMCEControl('add',id);
            saveFieldSequence();
        }
    }
    else if(direction == 'down'){
        var downId = $('#formPart-'+fieldId).next().attr('id');
        if(downId != 'undefined'){
            id = getFieldId(downId);
            tinyMCEControl('remove',id);
            $('li#formPart-'+fieldId).before($('li#'+downId));
            tinyMCEControl('add',id);
            saveFieldSequence();
        }
    }
}

function tinyMCEControl(action, id){
    if($('#tinyMCE-'+id).length != 0){
        tinyMCE.execCommand((action == 'remove' ? 'mceRemoveControl' : 'mceAddControl'), false, 'tinyMCE-'+id);
    }
}

function saveFieldSequence(){
    var post = new Object;
    var url = "./field.php?action=setFieldSequence";
    if($("input#itemId").val() == ""){
        createNewItem();
    }
    post["itemId"] = $("input#itemId").val();
    post["sequence"] = new Object;
    $('ul#formCanvas').children('li').each(function(i){
        post["sequence"][i] = getFieldId(this.id);
    });
    $.post(url,post);
}

function formOnLoad(){
    $('#panel').tabs();
    $('.form.radio').buttonset();   
}

function sortableInit(fieldId){
     $('#'+fieldId).sortable({
        start: function(event, ui){
            if(this.id == 'formCanvas'){
                $(ui.item).addClass('dragged');
            }
        },
        stop: function(event, ui){
            if(this.id == 'formCanvas'){
                $(ui.item).removeClass('dragged');
            }
        },
        update: function(event, ui) {
            var post = new Object;
            var url = "./form.php?action=setFormSequence";
            post['fieldId'] = getFieldId($(this).attr('id'));
            post["sequence"] = new Object;
            $(this).children('li').each(function(i){
                post["sequence"][i] = getFieldId(this.id);
            });
            $.post(url,post);
        }
    });
}

function setDisplay(type, fieldId){
    var url = './field.php?action=setDisplay';
    var post = new Object;
    if($("input#itemId").val() == ""){
        createNewItem();
    }
    post["itemId"] = $("input#itemId").val();
    post['fieldId'] = fieldId;
    if(type == 'list') post['newStatus'] =  $('input#listDisplay-'+fieldId).is(':checked') == true ? 1 : 0;
    else post['newStatus'] = $('input#detailsDisplay-'+fieldId).is(':checked')  == true ? 1 : 0;
    post['type'] = type;
    $.post(url, post, function(response){
        if(response == 1) $('li#formPart-'+fieldId).removeClass('disabled');
        else{
            $('li#formPart-'+fieldId).addClass('disabled');
            removeFormPart(fieldId);
        } 
    });
}

function addFormPart(fieldId){
    var url = './form.php?action=addFormPart';
    var post = {'fieldId':fieldId, 'itemId':$("input#itemId").val()};
    $('li#inactive-'+fieldId).remove();
    $.post(url, post, function(response){
        $('ul#formCanvas').append(response);
    });
}

function removeFormPart(fieldId){
    var url = './form.php?action=removeFormPart';
    var post = {'fieldId':fieldId};
    $('li#formPart-'+fieldId).remove();
    $.post(url, post, function(response){
        $('ul#inactive').append(response);
    });
}